package com.woniu.rear.maintenance.data.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniu.maintenance.data.model.domain.HousingMaterial;
import com.woniu.maintenance.data.model.dto.HousingMaterialDto;
import com.woniu.maintenance.data.model.param.SaveHousingMaterialParam;
import com.woniu.rear.comm.core.code.ConstData;
import com.woniu.rear.comm.core.dto.ResponseEntity;
import com.woniu.rear.comm.web.util.BeanCopyUtil;
import com.woniu.rear.comm.web.util.ServletUtil;
import com.woniu.rear.maintenance.data.web.service.HousingMaterialService;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class HousingMaterialController {
	
	@Autowired
	private HousingMaterialService housingMaterialService;
	
	@GetMapping("/list")
	public ResponseEntity<List<HousingMaterialDto>> list() {
		log.info("日志测试,token:",ServletUtil.getRequest().getHeader(ConstData.TOKEN));
		List<HousingMaterial> hms = housingMaterialService.list();
		List<HousingMaterialDto> hmds = BeanCopyUtil.copyList(hms, HousingMaterialDto::new);
		return ResponseEntity.BuildSuccessList(HousingMaterialDto.class).setData(hmds);
	}
	
	@PostMapping("/save")
	public ResponseEntity<?> save(SaveHousingMaterialParam saveHousingMaterialParam) {
		HousingMaterial hm = BeanCopyUtil.copyObject(saveHousingMaterialParam, HousingMaterial::new);
		boolean result = housingMaterialService.saveOrUpdate(hm);
		return result?ResponseEntity.BuildSuccess():ResponseEntity.BuildError();
	}
	
}



